/*
 *  Copyright (C) void Team 2010 Pedro Batista
 * 
 *  http://code.google.com/p/capacitec
 * 
 *  This file is part of capacitec.
 * 
 *  capacitec is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  capacitec is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with capacitec.  If not, see <http://www.gnu.org/licenses/>.
 */

package controller;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import dao.CursoDao;
import dao.ProgramaDao;
import dao.SubProgramaDao;
import entidades.Curso;
import seguranca.anotacoes.RestritoCoordenador;
import util.ProgramaESubProgramaList;

/**
 *
 * @author pedro
 */
@Resource
public class CursoController {

	private final CursoDao cursoDao;
	private final SubProgramaDao subProgramaDao;
	private final ProgramaDao programaDao;
	private final Result result;

	/**
	 * @param cursoDao
	 * @param subProgramaDao
	 * @param programaDao
	 * @param result
	 */
	public CursoController(CursoDao cursoDao, SubProgramaDao subProgramaDao, ProgramaDao programaDao, Result result) {
		this.cursoDao = cursoDao;
		this.subProgramaDao = subProgramaDao;
		this.programaDao = programaDao;
		this.result = result;
	}

	/**
	 * Mostra formulario com dados para cadastrar novo curso.
	 * @return Lista de programas e subprogramas para vincular o curso.
	 */
	@Get
	@Path("/curso/novo")
	@RestritoCoordenador
	public ProgramaESubProgramaList formulario(){
		//TODO: criar filtro pra mostrar apenas subprogramas que o coordenador coordena.
		return new ProgramaESubProgramaList(programaDao.carrega(), subProgramaDao.carrega());
	}

	/**
	 * Adiciona um novo curso ao banco.
	 * @param curso curso a ser adicionado.
	 */
	@Post
	@Path("/curso")
	@RestritoCoordenador
	public void adiciona(Curso curso){
		//TODO: adicionar sugestão perguntado se deseja adicionar uma única
		//turma com os dados do curso e então mostrar formulario para adicionar
		//turma parcialmente preenchido
		cursoDao.salva(curso);
		result.redirectTo(TurmaController.class).formulario();
	}
}
